package com.yixun.qfbao.hander;

import com.yixun.qfbao.api.service.PersonnelService;
import com.yixun.qfbao.apidto.ResetPwdByForgetReqDto;
import com.yixun.qfbao.apidto.ResetPwdResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.util.RedisUtil;
import com.yixun.qfbao.util.exception.TServerException;
import com.yixun.qfbao.vo.ResetPwdByForgetReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;


@Service("resetPwdByForgetHander")
public class ResetPwdByForgetHander extends CommonAbstractHander {
    private static Logger logger= LoggerFactory.getLogger(ResetPwdByForgetHander.class);

    @Autowired
    PersonnelService personnelService;
    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
        ResetPwdByForgetReqVo req = (ResetPwdByForgetReqVo) reqDto;
        ResetPwdByForgetReqDto resetPwdReqDto = req.getParams();
        BaseResponseDTO<ResetPwdResDto> res = new BaseResponseDTO<>();
        ResetPwdResDto resDto = new ResetPwdResDto();
        if (StringUtils.isEmpty(RedisUtil.get("ForgetPwd:"+resetPwdReqDto.getPhone(), String.class))) {
            resDto.setResult(ResultEnum.FAIL.getNo());
            resDto.setResultMsg("重置失效，请刷新后重试!");
            res.setResData(resDto);
            res.setResult(resDto.getResult());
            return res;
        }
        try {
            resDto = personnelService.resetPwdByForget(resetPwdReqDto);
        }catch (TServerException e){
            resDto.setResult(ResultEnum.FAIL.getNo());
            resDto.setResultMsg(e.getMessage());
        }catch (Exception e) {
            logger.error("UpdatePwdHander error: {}", e);
            resDto.setResult(ResultEnum.FAIL.getNo());
            resDto.setResultMsg("重置失败!");
        }
        res.setResData(resDto);
        res.setResult(resDto.getResult());
        return res;
    }
}
